package com.ruoyi.erp.controller;

import java.util.List;

import com.ruoyi.erp.domain.BatchReceivabelPlan;
import com.ruoyi.erp.domain.Project;
import com.ruoyi.erp.service.IProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.ReceivablePlan;
import com.ruoyi.erp.service.IReceivablePlanService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 收款计划Controller
 * 
 * @author sauger
 * @date 2022-03-28
 */
@Controller
@RequestMapping("/erp/receivable")
public class ReceivablePlanController extends BaseController
{
    private String prefix = "erp/receivable";

    @Autowired
    private IReceivablePlanService receivablePlanService;
    @Autowired
    private IProjectService projectService;

    @RequiresPermissions("erp:receivable:view")
    @GetMapping()
    public String receivable()
    {
        return prefix + "/receivable";
    }

    /**
     * 查询收款计划列表
     */
    @RequiresPermissions("erp:receivable:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ReceivablePlan receivablePlan)
    {
        startPage();
        List<ReceivablePlan> list = receivablePlanService.selectReceivablePlanList(receivablePlan);
        return getDataTable(list);
    }

    /**
     * 导出收款计划列表
     */
    @RequiresPermissions("erp:receivable:export")
    @Log(title = "收款计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ReceivablePlan receivablePlan)
    {
        List<ReceivablePlan> list = receivablePlanService.selectReceivablePlanList(receivablePlan);
        ExcelUtil<ReceivablePlan> util = new ExcelUtil<ReceivablePlan>(ReceivablePlan.class);
        return util.exportExcel(list, "收款计划数据");
    }

    /**
     * 新增收款计划
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存收款计划
     */
    @RequiresPermissions("erp:receivable:add")
    @Log(title = "收款计划", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BatchReceivabelPlan batchReceivabelPlan)
    {
        Project project = projectService.selectProjectById(batchReceivabelPlan.getProjectId());
        return toAjax(receivablePlanService.batchInsertReceivablePlans(project,batchReceivabelPlan));
    }

    /**
     * 修改收款计划
     */
    @RequiresPermissions("erp:receivable:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        ReceivablePlan receivablePlan = receivablePlanService.selectReceivablePlanById(id);
        mmap.put("receivablePlan", receivablePlan);
        return prefix + "/edit";
    }

    /**
     * 修改保存收款计划
     */
    @RequiresPermissions("erp:receivable:edit")
    @Log(title = "收款计划", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ReceivablePlan receivablePlan)
    {
        return toAjax(receivablePlanService.updateReceivablePlan(receivablePlan));
    }

    /**
     * 删除收款计划
     */
    @RequiresPermissions("erp:receivable:remove")
    @Log(title = "收款计划", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(receivablePlanService.deleteReceivablePlanByIds(ids));
    }
}
